import 'package:flutter/material.dart';
import 'package:flutter_project01/login/index.dart';
import 'package:flutter_project01/pages/home/notice_detail.dart';
import 'package:flutter_project01/pages/tabs_page.dart';
import 'package:flutter_project01/profile/index.dart';
import 'package:flutter_project01/utils/TokenManager.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        // 路由表
        '/': (context) => const TabsPage(), // 首页
        '/notice_detail': (context) => const NoticeDetail(), // 公告详情
        //  '/login': (context) => const LoginPage(), // 登录
      },
      onGenerateRoute: (RouteSettings settings) {
        // 获取token判断是否登陆
        final token = tokenManager.getToken();
        if (token.isEmpty) {
          // 无token 跳转到登陆页
          return MaterialPageRoute(builder: (context) => const LoginPage());
        } else {
          // 有token 判断跳转到什么页面
          if (settings.name == '/profile') {
            return MaterialPageRoute(builder: (context) => const ProfilePage());
          }
        }
        return null;
      },
    );
  }
}
